require "Cocos2d"
require "AudioEngine"

cc.FileUtils:getInstance():addSearchPath("src")
cc.FileUtils:getInstance():addSearchPath("res")
cc.FileUtils:getInstance():addSearchPath("res/image")
    
require "preDefine"
require "util/log"

-- cclog
local cclog = function(...)
    print(string.format(...))
end

-- for CCLuaEngine traceback
function __G__TRACKBACK__(msg)
    local dbgInfo = debug.traceback()
    
    cclog("----------------------------------------")
    cclog("LUA ERROR: " .. tostring(msg) .. "\n")
    cclog(dbgInfo)
    cclog("----------------------------------------")
    
    
    local errstr = "LUA ERROR: " .. tostring(msg) .. "\n"
    errstr = errstr..dbgInfo.."\n"
    log.append(errstr)
    return msg
end


local function main()
    collectgarbage("collect")
    -- avoid memory leak
    collectgarbage("setpause", 100)
    collectgarbage("setstepmul", 5000)
    
    --用户存档使用lua格式，所以这里将文档路径也加进来
    --cc.FileUtils:getInstance():addSearchPath(cc.FileUtils:getInstance():getWritablePath())

    cc.Director:getInstance():getOpenGLView():setDesignResolutionSize(480, 800, 0)
     
    --create scene 
--    local Scene = require("GameScene")
--    local gameScene = Scene.create() 
--    gameScene:readLevel(1)
--    
--    gameScene:prepareBullet_once()
--    
--    gameScene:playBgMusic()
    
--    if cc.Director:getInstance():getRunningScene() then
--        cc.Director:getInstance():replaceScene(gameScene.view)
--    else
--        cc.Director:getInstance():runWithScene(gameScene.view)
--    end

    



    local gameUtil = require "gameUtil"

    if cc.PLATFORM_OS_ANDROID == g_targetPlatform then
        gameData:checkSwichInfo_callJava()  --从后台获取某些开关信息
        pchCenter:setAndroidChannelAndInit(pchCfg.channel.glt)
    end

    gameUtil.readUserRecord()
    gameUtil.initDataAndSettings()


    --load res
    resMgr.loadRes_game()
    
    if cc.PLATFORM_OS_ANDROID == g_targetPlatform and DEF_CHN_XNY then
        local cs = require "purchase/channelSpecial"
        cs.XNY:getUserId_callJava()
    else
        gameUtil.changeScene("start")
        gameUtil.sendGameStat(et.GL_Start)
    end
    
             
end

function gameStartCallback()
    gameUtil.changeScene("start")
    gameUtil.sendGameStat(et.GL_Start)
end
        

local status, msg = xpcall(main, __G__TRACKBACK__)
if not status then
    error(msg)
end
